package com.starleague.battletech.unit.mech.jpa.appengine;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import com.starleague.battletech.unit.IWeight;
import com.starleague.battletech.unit.jpa.appengine.BattletechUnitComponentGae;
import com.starleague.battletech.unit.mech.IMechGyro;
import com.starleague.battletech.unit.mech.MechGyroType;

@Entity(name = "MECH_GYRO")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class MechGyroGae
        extends BattletechUnitComponentGae
        implements IMechGyro
{
    // Remember, AppEngine doesn't support FetchType.EAGER / Joins
    @OneToOne(mappedBy = "gyro", fetch = FetchType.LAZY, optional = false)
    private MechGae mech;

    @Basic(optional = false)
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private MechGyroType gyroType;

    @OneToMany(mappedBy = "gyro", cascade = CascadeType.ALL)
    private List<MechGyroCriticalSlotGae> criticalSlots;

    MechGyroGae(MechGae mech,
                IWeight weight,
                MechGyroType gyroType)
    {
        super(weight);
        this.mech = mech;
        this.gyroType = gyroType;
        this.criticalSlots = new ArrayList<>();
    }

    protected MechGyroGae()
    {
        //
    }

    @Override
    public MechGae getMech()
    {
        return this.mech;
    }

    @Override
    public boolean isHittable()
    {
        return true;
    }

    @Override
    public MechGyroType getGyroType()
    {
        return this.gyroType;
    }

    @Override
    protected List<MechGyroCriticalSlotGae> getCriticalSlots()
    {
        return this.criticalSlots;
    }

    @Override
    public Map<String, Object> infoProperties()
    {
        Map<String, Object> infoPropertyMap = super.infoProperties();
        infoPropertyMap.put("gyroType", this.gyroType);
        return infoPropertyMap;
    }
}
